package com.itic.system.user.address.service;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.system.region.persistence.model.HppRegion;
import com.itic.system.region.service.HppRegionService;
import com.itic.system.user.address.persistence.model.UserAddress;
import com.itic.system.user.address.persistence.model.UserAddressExample;
import com.itic.system.user.address.persistence.model.UserAddressExample.Criteria;
import com.itic.system.vo.UserAddressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.user.address.persistence.dao.UserAddressDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class UserAddressService
     extends CrudService<UserAddressDao,UserAddress,UserAddressExample> {
    @Autowired
    private UserAddressDao userAddressDao;
    @Autowired
    private HppRegionService  hppRegionService;

    /**
     * 添加或者编辑收货地址
     * @param addressId
     * @param userId
     * @param regionId
     * @param reciveName
     * @param recivePhone
     * @param zipCode
     * @param address
     * @return
     */
    public Map<String,Object> addOrEdit (String addressId,String userId,String regionId,String reciveName,String recivePhone,String zipCode,String address){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        HppRegion  region = this.hppRegionService.get(regionId);
        HppRegion cityRegion = this.hppRegionService.get(region.getParentRegionId());
        HppRegion provinceRegion = this.hppRegionService.get(cityRegion.getParentRegionId());
        if(region!=null && cityRegion !=null && provinceRegion!=null){
            if(StringUtils.isEmpty(addressId)){
                //添加收货地址
                this.addUserAddress(userId,region.getId(),region.getRegionName(),cityRegion.getId(),cityRegion.getRegionName(),provinceRegion.getId(),provinceRegion.getRegionName(),reciveName,recivePhone,zipCode,address);
                //返回成功
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            }else{
                //编辑收货地址
                UserAddress userAddress = this.get(addressId);
                if(userAddress!=null){
                    this.editUserAddress(userAddress,region.getId(),region.getRegionName(),cityRegion.getId(),cityRegion.getRegionName(),provinceRegion.getId(),provinceRegion.getRegionName(),reciveName,recivePhone,zipCode,address);
                    //返回成功
                    resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                    resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
                }else{
                    // 提示收货地址不存在
                    resultMap.put(BaseCode.CODE, BaseCode.HPP_USER_ADDRESS_NOT_EXIST_CODE);
                    resultMap.put(BaseCode.MSG, BaseCode.HPP_USER_ADDRESS_NOT_EXIST_MSG);
                }
            }
        }else{
            //提示该地区不存在
            resultMap.put(BaseCode.CODE, BaseCode.HPP_REGION_NOT_EXIST_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.HPP_REGION_NOT_EXIST_MSG);
        }
        return resultMap;
    }

    /**
     * 添加收货地址
     * @param userId
     * @param regionId
     * @param regionName
     * @param cityId
     * @param cityName
     * @param provinceId
     * @param provinceName
     * @param reciveName
     * @param recivePhone
     * @param zipCode
     * @param address
     * @return
     */
    public String addUserAddress(String userId,String regionId,String regionName,String cityId,String cityName,String provinceId,String provinceName,String reciveName,String recivePhone,String zipCode, String address){
        UserAddress userAddress = new UserAddress();
        userAddress.setAddress(address);
        userAddress.setUserId(userId);
        userAddress.setRegionId(regionId);
        userAddress.setRegionName(regionName);
        userAddress.setCityId(cityId);
        userAddress.setCityName(cityName);
        userAddress.setProvinceId(provinceId);
        userAddress.setProvinceName(provinceName);
        userAddress.setReceiverName(reciveName);
        userAddress.setReceiverPhone(recivePhone);
        userAddress.setZipCode(zipCode);
        String result = this.add(userAddress);
        return result;
    }

    /**
     * 编辑收货地址
     * @param userAddress
     * @param regionId
     * @param regionName
     * @param cityId
     * @param cityName
     * @param provinceId
     * @param provinceName
     * @param reciveName
     * @param recivePhone
     * @param zipCode
     * @param address
     * @return
     */
    public String editUserAddress(UserAddress userAddress,String regionId,String regionName,String cityId,String cityName,String provinceId,String provinceName,String reciveName,String recivePhone,String zipCode, String address){
        userAddress.setAddress(address);
        userAddress.setRegionId(regionId);
        userAddress.setRegionName(regionName);
        userAddress.setCityId(cityId);
        userAddress.setCityName(cityName);
        userAddress.setProvinceId(provinceId);
        userAddress.setProvinceName(provinceName);
        userAddress.setReceiverName(reciveName);
        userAddress.setReceiverPhone(recivePhone);
        userAddress.setZipCode(zipCode);
        String result = this.edit(userAddress);
        return result;
    }

    /**
     * 根据用户分页查询用户收货地址
     * @param example
     * @param userId
     * @return
     */
    public List<UserAddress> findUserAddressByUserIdAndPage(UserAddressExample example , String userId){
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andUserIdEqualTo(userId);
        return this.list(example);
    }

    /**
     * 查询收货地址业务封装
     * @param example
     * @param userId
     * @return
     */
    public List<Map<String ,Object>>  findUserAddress(UserAddressExample example , String userId){
        List<UserAddress> userAddresses = this.findUserAddressByUserIdAndPage(example,userId);
        List<Map<String ,Object>> list = new ArrayList<>();
        for(UserAddress userAddress : userAddresses){
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("name",userAddress.getReceiverName());
            data.put("phone",userAddress.getReceiverPhone());
            String region = userAddress.getProvinceName() + userAddress.getCityName() + userAddress.getRegionName();
            data.put("region",region);
            data.put("address",userAddress.getAddress());
            data.put("addressId",userAddress.getId());
            list.add(data);
        }
        return list;
    }

    /**
     * 删除用户收货地址
     * @param userId
     * @param addressId
     * @return
     */
    public Map<String ,Object>  delUserAddress(String userId, String addressId){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        UserAddress userAddress = this.get(addressId);
        if(userAddress !=null){
            if(userId.equals(userAddress.getUserId())){
                this.delete(userAddress);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            }else{
                //提示用户无权操作
                resultMap.put(BaseCode.CODE, BaseCode.HPP_SELF_USER_NOT_ACCORDANCE_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.HPP_SELF_USER_NOT_ACCORDANCE_MSG);
            }
        }else{
            //提示用户地址不存在
            resultMap.put(BaseCode.CODE, BaseCode.HPP_USER_ADDRESS_NOT_EXIST_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.HPP_USER_ADDRESS_NOT_EXIST_MSG);
        }
        return resultMap;
    }

    /**
     * 地址详情
     * @param addressId
     * @return
     */
    public UserAddressVo getAddressDetail(String addressId){
        UserAddress userAddress =  this.get(addressId);
        UserAddressVo userAddressVo = new UserAddressVo();
        if(userAddress!=null){
            userAddressVo.setAddress(userAddress.getAddress());
            userAddressVo.setCity(userAddress.getCityName());
            userAddressVo.setCityId(userAddress.getCityId());
            userAddressVo.setRegion(userAddress.getRegionName());
            userAddressVo.setRegionId(userAddress.getRegionId());
            userAddressVo.setProvince(userAddress.getProvinceName());
            userAddressVo.setProvinceId(userAddress.getProvinceId());
            userAddressVo.setReciveName(userAddress.getReceiverName());
            userAddressVo.setRecivePhone(userAddress.getReceiverPhone());
            userAddressVo.setZipCode(userAddress.getZipCode());
        }
        return userAddressVo;
    }



}